import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shishi/login/login.dart';
import 'package:shishi/page/CardPage.dart';
import 'package:shishi/page/CollectPage.dart';
import 'package:shishi/page/Kefu.dart';
import 'package:shishi/page/NaoJingJIZhuanWang.dart';
import 'package:shishi/page/Songci.dart';
import 'package:shishi/page/gua_card_page.dart';
import 'package:shishi/page/wendapage.dart';

import '../Colors.dart';

class DrawerPage extends StatefulWidget {
  @override
  _DrawerPageState createState() => _DrawerPageState();
}

class _DrawerPageState extends State<DrawerPage> {
  SharedPreferences sharedPreferences;
  String _name;
  String _phone;
  String _icon;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getUserInfo();
  }

  Future<String> _getUserInfo() async {
    sharedPreferences = await SharedPreferences.getInstance();
    _name = sharedPreferences.getString("name") ?? "";
    _phone = sharedPreferences.getString("phone") ?? "";
    setState(() {});
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          UserAccountsDrawerHeader(
            accountName: Text("$_name"),
            accountEmail: Text("$_phone"),
            currentAccountPicture: GestureDetector(
              child: ClipOval(
                child: Image.network(
                  'https://scpic.chinaz.net/files/pic/pic9/202105/bpic23217.jpg',
                  width: 80,
                  height: 80,
                  fit: BoxFit.cover,
                ),
              ),
              onTap: () {
                openFile();
              },
            ),
            onDetailsPressed: () {

            },
            otherAccountsPictures: <Widget>[],
          ),
          ListTile(
            leading: Icon(
              Icons.book,
              color: Colors.pink,
            ),
            title: Text("我的收藏"),
            onTap: () {
              Navigator.of(context)
                  .push(CupertinoPageRoute(builder: (context) => Collect()));
            },
          ),
          Divider(),
          ListTile(
            leading: Icon(
              FontAwesomeIcons.bookMedical,
              color: Colors.pink,
            ),
            title: Text("宋词"),
            onTap: () {
              Navigator.of(context)
                  .push(CupertinoPageRoute(builder: (context) => SongCiPage()));
            },
          ),
          Divider(),
          ListTile(
            leading: Icon(
              Icons.person_add,
              color: Colors.pink,
            ),
            title: Text("一站到底"),
            onTap: () {
              Navigator.of(context)
                  .push(CupertinoPageRoute(builder: (context) => WenDapage()));
            },
          ),
          Divider(),
          ListTile(
            leading: Icon(
              FontAwesomeIcons.mandalorian,
              color: Colors.pink,
            ),
            title: Text("脑筋急转弯"),
            onTap: () {
              Navigator.of(context).push(
                  CupertinoPageRoute(builder: (context) => NaoJingJiZhuan()));
            },
          ),
          Divider(),
          ListTile(
            leading: Icon(
              CupertinoIcons.creditcard_fill,
              color: Colors.pink,
            ),
            title: Text("翻卡游戏"),
            onTap: () {
              Navigator.of(context).push(
                  CupertinoPageRoute(builder: (context) => OpenCardPage()));
            },
          ),
          Divider(),
          ListTile(
            leading: Icon(
              Icons.person_add,
              color: Colors.pink,
            ),
            title: Text("智能客服"),
            onTap: () {
              Navigator.of(context)
                  .push(CupertinoPageRoute(builder: (context) => KeFu()));
            },
          ),
          Divider(),
          ListTile(
            leading: Icon(
              CupertinoIcons.color_filter,
              color: Colors.pink,
            ),
            title: Text("主题"),
            onTap: () {
              Get.changeTheme(Get.isDarkMode? ThemeData.light(): ThemeData.dark());
            },
          ),
          Divider(),
          ListTile(
            leading: Icon(
              FontAwesomeIcons.solidAddressCard,
              color: Colors.pink,
            ),
            title: Text("刮卡"),
            onTap: () {
              Navigator.of(context).push(
                CupertinoPageRoute(builder: (context) => GuaCard())
              );

            },
          ),
          Divider(),
          ListTile(
            leading: Icon(
              Icons.exit_to_app,
              color: Colors.amberAccent,
            ),
            title: Text("退出登陆"),
            onTap: () {
              sharedPreferences.clear();
              Navigator.of(context).pushAndRemoveUntil(
                CupertinoPageRoute(builder:(context)=>Login()),(route)=>route==null,
              );
            },
          ),
        ],
      ),
    );
  }


  void openFile() async {
    FilePickerResult result = await FilePicker.platform.pickFiles();
    if (result != null) {
      File file = File(result.files.single.path);
      print(file.path);
    } else {
      // User canceled the picker
    }
  }

}
